
Imports cmplib01


Public Class frmBattlegroupManager

    Const c_BGDist As Integer = 25    '// the generic distance for bg splitting (has to be higher than the minimum distance)


    Private ptrCGD As CampaignerGameData
    Private ptrBG As BattlegroupChit
    Private ptrFMP As FMPLib01.FlexMapData

    Public Shadows Sub showdialog(ByRef cgd As CampaignerGameData, ByRef bg As BattlegroupChit, ByRef fmp As FMPLib01.FlexMapData)
        ptrCGD = cgd
        ptrBG = bg
        ptrFMP = fmp

        ListMembers()

        picBGIcon.Image = ptrBG.icon
        lblBGName.Text = ptrBG.Name

        MyBase.ShowDialog()
    End Sub

    Private Sub ListMembers()
        lstUnits.Items.Clear()
        Try

            '// list it like a man
            For Each mem As ChitMember In ptrBG.members
                Dim litem As New ListViewItem
                litem.Tag = mem
                litem.Text = mem.Name
                If mem.UnitType = GroundChitClass_cgd.Battlegroup Then
                    litem.ImageIndex = 0
                ElseIf mem.UnitType = GroundChitClass_cgd.Unit Then
                    litem.ImageIndex = 1
                End If
                lstUnits.Items.Add(litem)
            Next

            '// make the nearby battlegroups
            makeMenu()

            If ptrBG.overridePointValue > 0 Then
                lblPV.Text = "Point Value: " & ptrBG.overridePointValue & " override"
            Else
                lblPV.Text = "Point Value: " & ptrBG.SuggestedPointValue
            End If


        Catch ex As Exception
            HandleException("frmBattlegroupManager", ex)
        End Try
    End Sub

    Private Sub makeMenu()
        Me.mnuSplitBG.DropDownItems.Clear()

        Try

            Dim oclick As New System.EventHandler(AddressOf menuclick)
            Dim index As Integer

            For Each mem As ChitMember In ptrCGD.Chits
                If mem.UnitType = GroundChitClass_cgd.Battlegroup Then
                    If mem.OwningPlayer.Equals(ptrBG.OwningPlayer) Then
                        '// now, we can distance check 'em,  AND the item is not the same item that is being split FROM... hahah.
                        If XYDistance(mem.Location, ptrBG.Location) <= c_BGDist Then 'AndAlso (Not (mem.Equals(ptrBG))) Then
                            If Not mem.Equals(ptrBG) Then
                                '// yay! We can add one!
                                Dim tip As Windows.Forms.ToolStripItem
                                'tip = New ToolStripDropDownButton(mem.Name, mem.icon, oclick)
                                'tip.Tag = mem
                                'tip.Text = mem.Name
                                'mnuSplitBG.DropDownItems.Add(tip)
                                mnuSplitBG.DropDownItems.Add(mem.Name, mem.icon, oclick)
                                tip = mnuSplitBG.DropDownItems.Item(index)
                                tip.Tag = mem
                                index += 1
                            End If
                        End If
                    End If
                End If
            Next

            '// if there are no nearby members, disable the first item
            If mnuSplitBG.DropDownItems.Count = 0 Then
                mnuSplitBG.Enabled = False
            Else
                mnuSplitBG.Enabled = True
            End If

        Catch ex As Exception
            HandleException("frmBattlegroupManager", ex)
        End Try

    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        '// simply closes
        Me.Hide()
    End Sub

    Private Sub menuclick(ByVal sender As Object, ByVal e As System.EventArgs)
        '// dis be where we go when dey click
        '// all selected units will be moved. AFter point value checking.
        '// also, the units MUST be able to move from point A to point B... No jumping rivers.

        Try
            Dim target As BattlegroupChit = sender.tag   '// the target battlegroup, sent in from the menu item's tag. 
            Dim canDo As Boolean = False    '// if all unit types CAN DO, this is a CAN DO!! Yah!
            Dim pv As Integer '// the point value of selected items
            Dim movemembers As New ChitMembers '// our holding tank

            '// make sure both the target and source are in the correct status to pull off a split...
            Select Case target.Status
                Case GroundUnitStatus_cgd.OK, GroundUnitStatus_cgd.Moving, GroundUnitStatus_cgd.Regrouped
                    canDo = True
            End Select

            Select Case ptrBG.Status
                Case GroundUnitStatus_cgd.Assembling, GroundUnitStatus_cgd.OK, GroundUnitStatus_cgd.Moving, GroundUnitStatus_cgd.Regrouped
                    canDo = True
            End Select

            If canDo = True Then
                canDo = False     '// but continue...
            Else
                MessageBox.Show("Either one or both battlegroups have incorrect status to be split or combined.", "Can't Split", MessageBoxButtons.OK)
                Exit Sub
            End If

            '// make sure we can make it. awesome.
            For Each lit As ListViewItem In lstUnits.SelectedItems
                Dim chitmem As ChitMember = lit.Tag
                Select Case chitmem.UnitType
                    Case GroundChitClass_cgd.Battlegroup
                        canDo = ptrFMP.MovementFromAToBAllowed(ptrBG.Location, target.Location, DirectCast(chitmem, BattlegroupChit).movementClassID)
                        '// 27 aug
                        DirectCast(chitmem, BattlegroupChit).calculateProperties()
                        pv += DirectCast(chitmem, BattlegroupChit).SuggestedPointValue
                    Case GroundChitClass_cgd.Unit
                        canDo = ptrFMP.MovementFromAToBAllowed(ptrBG.Location, target.Location, DirectCast(chitmem, ChitUnitTypeMember).ptrUnitType.MovementClassID)
                        pv += DirectCast(chitmem, ChitUnitTypeMember).ptrUnitType.SuggestedPointValue
                    Case Else
                        canDo = False
                End Select
                If canDo = False Then
                    MessageBox.Show("Not all selected members can move due to terrain restrictions.", "Invalid Move", MessageBoxButtons.OK)
                    Exit Sub
                End If

                '// here we'll add it to a collection
                movemembers.add(chitmem)
            Next

            '// verify point value
            If target.CurrentPV + pv > ptrCGD.BattlegroupMaxPointValue AndAlso ptrCGD.BattlegroupMaxPointValue <> 0 Then
                MessageBox.Show("Adding the selected items will put the target Battlegroup above the maximum.", "Invalid PV", MessageBoxButtons.OK)
                Exit Sub
            End If

            '// if we get here, we're ok to make the move!
            ptrBG.Status = GroundUnitStatus_cgd.Assembling
            target.Status = GroundUnitStatus_cgd.Assembling

            '// make the move
            For Each mem As ChitMember In movemembers
                ptrBG.members.remove(mem)
                target.members.add(mem)
            Next

            ptrBG.calculateProperties()
            target.calculateProperties()

            '// also the deployed point value will match the suggested point values. The user will have to manually change it if it isn't right.
            If ptrBG.overridePointValue = 0 Then
                ptrBG.deployedpointvalue = ptrBG.SuggestedPointValue
            Else
                '// allow them to override the point value
                
            End If

            If target.overridePointValue = 0 Then
                target.deployedpointvalue = target.SuggestedPointValue
            Else
                '// we'll have to override the point value

            End If


            '// now that we did that, we need to relist this battlegroup's stuff.
            ListMembers()

        Catch ex As Exception
            HandleException("frmBattlegroupManager", ex)
        End Try
    End Sub

    Private Sub mnuSplitNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuSplitNew.Click
        '// create a new battlegroup and put the units in it, easy peasy.
        Try
            '// it will have the same location as the "parent", and the same icon. I don't think that's a problem.
            '// we have to enter a name, and set the icon before continuing
            Dim members As New ChitMembers

            For Each lit As ListViewItem In lstUnits.SelectedItems
                Dim chitmem As ChitMember = lit.Tag
                members.add(chitmem)
            Next

            If members.Count = 0 Then Exit Sub

            Dim fnew As New frmSplitNewBG
            fnew.showDialog(ptrCGD, ptrBG, members)

            For Each mem As ChitMember In members
                If Not mem.Status = GroundUnitStatus_cgd.Dead Then ptrBG.members.remove(mem)
            Next

            If ptrBG.members.Count = 0 Then ptrBG.Status = GroundUnitStatus_cgd.Disbanded


            ptrBG.calculateProperties()
            ListMembers() '// in case we're not done yet... if they want to add to different groups... yah.


        Catch ex As Exception
            HandleException("frmBattlegroupManager", ex)
        End Try
    End Sub

    Private Sub lstUnits_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstUnits.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Right Then
            If lstUnits.SelectedItems.Count > 0 Then
                ContextMenuStrip1.Show(lstUnits, e.X, e.Y)
            End If
        End If
    End Sub
End Class